package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyMqConfig {
    public static final String ORDER_EVENT_EXCHAGE = "order-event-exchage";
    public static final String ORDER_DELAY_QUQUQ = "order.delay.queue";
    public static final String ORDER_RELEASE_ORDER_QUEUE = "order.release.order.queue";
    public static final Integer MESSAGE_TTL = 60000;
    public static final String DELAY_QUEUE_ROUTINGKEY = "order.create.order";
    public static final String ORDER_RELEASE_ORDER_ROUTINGKEY = "order.release.order";

    public static final String DEAD_QUEUE = "stock.release.stock.queue";
    public static final String ORDER_RELEASE_OTHER = "order.release.other.#";

    public static final String SECKILL_QUEUE= "order.seckill.order.queue";

//    @RabbitListener(queues = ORDER_RELEASE_ORDER_QUEUE)
//    public void listen(OrderEntity orderEntity, Channel channel, Message message){
//        System.out.println("收到过期的订单信息，准备关闭订单："+orderEntity.getOrderSn());
//        try {
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    @Bean
    public TopicExchange orderEventExchange() {
        TopicExchange topicExchange = new TopicExchange(ORDER_EVENT_EXCHAGE, true, false);
        return topicExchange;
    }

    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        // 设置死信交换机
        arguments.put("x-dead-letter-exchange", ORDER_EVENT_EXCHAGE);
        // 设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key", ORDER_RELEASE_ORDER_ROUTINGKEY);
        // 设置TTL
        arguments.put("x-message-ttl", MESSAGE_TTL);
        return QueueBuilder.durable(ORDER_DELAY_QUQUQ).withArguments(arguments).build();
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return QueueBuilder.durable(ORDER_RELEASE_ORDER_QUEUE).build();
    }

    @Bean
    public Binding normalBinding() {
        Binding binding = new Binding(ORDER_DELAY_QUQUQ,
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHAGE,
                DELAY_QUEUE_ROUTINGKEY,
                null);
        return binding;
    }

    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(orderReleaseOrderQueue()).to(orderEventExchange()).with(ORDER_RELEASE_ORDER_ROUTINGKEY);
    }

    @Bean
    public Binding orderRelaseOtherBinding() {
        return new Binding(DEAD_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHAGE,
                ORDER_RELEASE_OTHER,
                null);
    }


    @Bean
    public Queue secKillQueue(){
        return QueueBuilder.durable(SECKILL_QUEUE).build();
    }

    @Bean
    public Binding secKillQueueBinding(){
        return BindingBuilder.bind(secKillQueue()).to(orderEventExchange()).with("order.seckill.order");
    }
}
